<?php
namespace app\shop\controller;
use app\ApiRest;

use app\farm\model\ShopGoods;
use app\massage\model\Coupon;
use app\massage\model\CouponRecord;
use app\Rest;

use app\shop\model\Cap;
use app\shop\model\Car;
use app\shop\model\Goods;
use app\shop\model\GoodsCate;
use app\shop\model\GoodsSpe;
use app\shop\model\GoodsSpePrice;
use app\shop\model\Integral;
use app\shop\model\LuckConfig;
use app\shop\model\LuckDraw;
use app\shop\model\LuckRecord;
use app\shop\model\OrderGoods;
use app\shop\model\SeckillConfig;
use app\shop\model\SeckillGoods;
use app\shop\model\SeckillInfo;
use app\shop\model\SeckillList;
use app\shop\model\User;
use think\App;

use think\facade\Db;
use think\Request;



class IndexGoods extends ApiRest
{

    protected $model;

    protected $cate_model;

    protected $kill_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new ShopGoods();

        $this->kill_model = new SeckillList();

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-22 16:33
     * @功能说明:秒杀活动
     */
    public function killAtvList(){

        $this->kill_model->initAtv();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $dis[] = ['end_time','>',time()];

        $data = $this->kill_model->where($dis)->order('start_time,id desc')->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-25 11:38
     * @功能说明:热门商品列表
     */
    public function hotGoodsList(){

        $input = $this->_param;

        $goods_model = new ShopGoods();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['index_show','=',1];

        $dis[] = ['status','=',1];

        if(!empty($input['goods_name'])){

            $dis[] = ['goods_name','like','%'.$input['goods_name'].'%'];

        }

        $data = $goods_model->where($dis)->order('top desc,id desc')->paginate(10)->toArray();

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-25 15:51
     * @功能说明:秒杀商品列表
     */
    public function killGoodsList(){

        $input = $this->_param;

        $goods_model = new SeckillGoods();

        $dis[] = ['a.atv_id','=',$input['atv_id']];

        $dis[] = ['b.status','=',1];

        $dis[] = ['a.status','=',1];

        $dis[] = ['e.status','=',1];

        $dis[] = ['d.status','=',2];

        $dis[] = ['d.business_status','=',1];

        if(!empty($input['goods_name'])){

            $dis[] = ['b.goods_name','like','%'.$input['goods_name'].'%'];

        }

        $data = $goods_model->goodsList($dis);

        if(!empty($data['data'])){

            $info_model = new SeckillInfo();

            foreach ($data['data'] as &$v){

                $dis = [

                    'uniacid'  => $this->_uniacid,

                    'goods_id' => $v['goods_id'],

                    'kill_id'  => $v['atv_id'],

                    'user_id'  => $this->getUserId()
                ];

                $find = $info_model->dataInfo($dis);

                $v['have_notice'] = !empty($find)?1:0;

            }
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-24 15:10
     * @功能说明:秒杀活动提醒
     */
    public function killNotice(){

        $input = $this->_input;

        $dis = [

            'uniacid'  => $this->_uniacid,

            'goods_id' => $input['goods_id'],

            'kill_id'  => $input['atv_id'],

            'user_id'  => $this->getUserId()

        ];

        $info_model = new SeckillInfo();

        $find = $info_model->dataInfo($dis);

        if(empty($find)){

            $info_model->dataAdd($dis);

        }else{

            $info_model->where($dis)->delete();
        }

        return $this->success(true);
    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-26 14:03
     * @功能说明:抽奖详情
     */
    public function luckInfo(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['start_time','<',time()];

        $dis[] = ['end_time','>',time()];

        $dis[] = ['status','=',1];

        $luck_model = new LuckDraw();

        $atv= $luck_model->dataInfo($dis);

        if(empty($atv)){

            return $this->success($atv);
            $this->errorMsg('活动已经结束');
        }

        $config_model = new LuckConfig();

        $record_model = new LuckRecord();

        $user_model   = new \app\farm\model\User();

        $list = $config_model->where(['luck_id'=>$atv['id']])->order('top,id desc')->select()->toArray();

        $user_num = $record_model->where(['user_id'=>$this->getUserId(),'atv_id'=>$atv['id']])->whereTime('create_time','today')->count();

        $arr['data'] = $list;

        $arr['user_num'] = ($atv['day_times'] - $user_num)>0?$atv['day_times'] - $user_num:0;

        $arr['integral'] = $atv['integral'];

        $arr['atv_id']   = $atv['id'];

        $arr['cover']    = $atv['cover'];

        $arr['text']     = $atv['text'];

        $arr['day_times']= $atv['day_times'];

        $arr['user_integral'] = $user_model->where(['id'=>$this->getUserId()])->sum('integral');

        return $this->success($arr);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-26 16:12
     * @功能说明:中奖记录
     */
    public function luckRecord(){

        $input = $this->_param;

        $record_model = new LuckRecord();

        $dis = [

            'a.atv_id' => $input['id'],

            'a.is_luck'=> 1
        ];

        $data = $record_model->recordList($dis);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-25 11:04
     * @功能说明:用户中奖记录
     */
    public function userLuckRecord(){

        $input = $this->_param;

        $record_model = new LuckRecord();

        $dis = [

            'a.user_id' => $this->getUserId(),

         //   'a.is_luck'=> 1
        ];

        if(!empty($input['id'])){

            $dis['a.atv_id'] = $input['id'];
        }

        $data = $record_model->recordList($dis);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-26 14:09
     * @功能说明:用户抽奖
     */
    public function luckDraw(){

        $input = $this->_input;

        $luck_model = new LuckDraw();

        $record_model = new LuckRecord();

        $dis[] = ['id','=',$input['id']];

        $dis[] = ['start_time','<',time()];

        $dis[] = ['end_time','>',time()];

        $data = $luck_model->dataInfo($dis);

        if(empty($data)){

            $this->errorMsg('活动未在规则时间内');
        }

        $user_num = $record_model->where(['user_id'=>$this->getUserId(),'atv_id'=>$input['id']])->whereTime('create_time','today')->count();

        if($user_num>=$data['day_times']){

            $this->errorMsg('超过每日抽奖次数，你今日已抽'.$user_num.'次');
        }

        $user_model   = new \app\farm\model\User();

        $user_integral= $user_model->where(['id'=>$this->getUserId()])->sum('integral');

        if($data['integral']>$user_integral){

            $this->errorMsg('积分不足');
        }
        //抽奖
        $res = $luck_model->luckDraw($input['id'],$this->getUserId(),$data['integral'],$user_num);

        return $this->success($res);

    }


















}
